package com.iotp.controller.admin.organization;

import com.alibaba.fastjson.JSONObject;
import com.iotp.controller.admin.base.BaseController;
import com.iotp.entity.base.DataMessage;
import com.iotp.entity.base.KKpager;
import com.iotp.entity.organization.Organization;
import com.iotp.service.organization.OrganizationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iotp.utils.common.WyConstant;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletOutputStream;
import java.io.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 系统机构管理控制器
 * @author Assassin
 */
@Controller
@RequestMapping("/sys/org")
public class OrganizationController extends BaseController {
	
	@Autowired
	private OrganizationService organizationService;// 系统机构业务接口
	
	/**
	 * 进入系统机构列表页面
	 * @return sys/org/list_org
	 */
	@GetMapping("/list")
	public String gotoListPage() {
		return "admin/sys/org/list_org";
	}
	
	/**
	 * 进入系统根机构添加页面
	 * @return sys/org/add_root_org
	 */
	@GetMapping("/toAddRoot")
	public String gotoAddRootPage() {
		return "admin/sys/org/add_root_org";
	}
	
	/**
	 * 进入系统子机构添加页面
	 * @param orgPid 父级机构Id
	 * @return sys/org/add_sub_org
	 */
	@GetMapping("/toAddSub")
	public String gotoAddSubPage(@RequestParam(required = true, value = "orgPid") String orgPid) {
		// 后台数据验证
		if(!this.validateStringProperty(orgPid, 50, true)) {
			// TODO 错误页面
			return "redirect:/error";
		} 
		Organization parentOrg = this.organizationService.queryById(Long.parseLong(orgPid));
		if(parentOrg == null) {
			return "redirect:/error";
		}
		request.setAttribute("orgPid", orgPid);
		return "admin/sys/org/add_sub_org";
	}
	
	/**
	 * 进入系统机构编辑页面
	 * @param orgId 机构Id
	 * @return sys/org/edit_org
	 */
	@GetMapping("/toEdit")
	public String gotoEditPage(@RequestParam(required = true, value = "orgId") String orgId) {
		// 后台数据验证
		if(!this.validateStringProperty(orgId, 50, true)) {
			// TODO 错误页面
			return "redirect:/error";
		} 
		Organization editOrg = this.organizationService.queryById(Long.parseLong(orgId));
		if(editOrg == null) {
			return "redirect:/error";
		}
		request.setAttribute("editOrg", editOrg);
		return "admin/sys/org/edit_org";
	}
	
	/**
	 * 加载机构树表格数据
	 * @param pageNo 当前页码
	 * @param pageSize 每页记录数
	 * @return DataMessage
	 */
	@ResponseBody
	@PostMapping("/treeTable")
	public DataMessage selectTreeTable(@RequestParam(required = false, value = "pageNo") Integer pageNo,
			@RequestParam(required = false, value = "pageSize") Integer pageSize) {
		// 返回数据消息实体
		DataMessage dm = new DataMessage();
		List<Organization> resultList = new ArrayList<Organization>();
		PageHelper.startPage(getPageNo(pageNo), getPageSize(pageSize));
		List<Organization> orgList = this.organizationService.queryList(null, null, getOrgType(), null, 0,
				null, null, null, null, null, null, null, null, null, null,
				null, null, null, null, null, null, null, null, null,
				null, null);
		PageInfo<Organization> pageInfo = new PageInfo<Organization>(orgList);
		for(Organization org : orgList) {
			if(org.getOrgLevel().intValue() == 1) {
				this.recursionSubOrg(resultList, org);
			}
		}
		dm.setResult(WyConstant.RESULT_CODE_SUCCESS);
		dm.setData(resultList);
		KKpager pager = new KKpager(getPageNo(pageNo), pageInfo.getPages() , pageInfo.getTotal());
		dm.setMessage(JSONObject.toJSON(pager).toString());
		return dm;
	}
	
	/**
	 * 添加系统机构根机构
	 * @param orgName 机构名称
	 * @param orgAliasName 机构别名
	 * @param orgDesc 机构说明
	 * @param orgPhone 机构电话
	 * @param orgFax 机构传真
	 * @param orgWebsite 机构网址
	 * @param provinceId 机构省Id
	 * @param cityId 机构市Id
	 * @param districtId 机构区Id
	 * @param orgAddress 机构地址
	 * @param lng 机构经度
	 * @param lat 机构纬度
	 * @param orgManagerId 机构管理人员Id
	 * @param orgManagerName 机构管理人员名称
	 * @param orgManagerPhone 机构管理人员电话
	 * @return DataMessage
	 */
	@ResponseBody
	@PostMapping("/addRoot")
	public DataMessage addRoot(
			@RequestParam(required = true, value = "orgName") String orgName,
			@RequestParam(required = false, value = "orgAliasName") String orgAliasName,
			@RequestParam(required = false, value = "orgDesc") String orgDesc,
			@RequestParam(required = false, value = "orgPhone") String orgPhone,
			@RequestParam(required = false, value = "orgFax") String orgFax,
			@RequestParam(required = false, value = "orgWebsite") String orgWebsite,
			@RequestParam(required = false, value = "provinceId") String provinceId,
			@RequestParam(required = false, value = "cityId") String cityId,
			@RequestParam(required = false, value = "districtId") String districtId,
			@RequestParam(required = false, value = "orgAddress") String orgAddress,
			@RequestParam(required = false, value = "lng") String lng,
			@RequestParam(required = false, value = "lat") String lat,
			@RequestParam(required = false, value = "orgManagerId") String orgManagerId,
			@RequestParam(required = false, value = "orgManagerName") String orgManagerName,
			@RequestParam(required = false, value = "orgManagerPhone") String orgManagerPhone) {
		// 返回数据消息实体
		DataMessage dm = new DataMessage();
		// 后台数据验证
		if(!this.validateStringProperty(orgName, 50, true)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构名称参数错误");
		} else if(!this.validateStringProperty(orgAliasName, 50, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构别名别名参数错误");
		} else if(!this.validateStringProperty(orgDesc, 255, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构说明参数错误");
		} else if(!this.validateStringProperty(orgPhone, 50, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构电话参数错误");
		} else if(!this.validateStringProperty(orgFax, 50, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构传真参数错误");
		} else if(!this.validateStringProperty(orgWebsite, 255, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构网址参数错误");
		} else if(!this.validateStringProperty(provinceId, 50, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构省Id参数错误");
		} else if(!this.validateStringProperty(cityId, 50, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构市Id参数错误");
		} else if(!this.validateStringProperty(districtId, 50, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构区Id参数错误");
		} else if(!this.validateStringProperty(orgAddress, 255, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构地址参数错误");
		} else if(!this.validateStringProperty(lng, 15, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构经度参数错误");
		} else if(!this.validateStringProperty(lat, 15, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构纬度参数错误");
		} else if(!this.validateStringProperty(orgManagerId, 50, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构管理人员Id参数错误");
		} else if(!this.validateStringProperty(orgManagerName, 50, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构管理人员名称参数错误");
		} else if(!this.validateStringProperty(orgManagerPhone, 50, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构管理人员电话参数错误");
		} else {
			BigDecimal lngBd = null;
			if(lng != null && !"".equals(lng.trim())) {
				lngBd = new BigDecimal(lng);
				lngBd.setScale(6, BigDecimal.ROUND_HALF_UP);
			}
			BigDecimal latBd = null;
			if(lat != null && !"".equals(lat.trim())) {
				latBd = new BigDecimal(lat);   
				latBd.setScale(6, BigDecimal.ROUND_HALF_UP);
			}
			Long orgManagerIdLong = (orgManagerId == null || "".equals(orgManagerId.trim())) ? null : Long.parseLong(orgManagerId);
			Long orgId = this.organizationService.addRoot(getOrgType(), orgName, orgAliasName, orgDesc, orgPhone, orgFax,
					orgWebsite, provinceId, cityId, districtId, orgAddress, lngBd, latBd, orgManagerIdLong, orgManagerName,
					orgManagerPhone);
			dm.setResult(WyConstant.RESULT_CODE_SUCCESS);
			dm.setData(orgId);
			dm.setMessage("根机构添加成功");
		}
		return dm;
	}
	
	/**
	 * 添加系统机构子机构
	 * @param orgPid 机构父级Id
	 * @param orgName 机构名称
	 * @param orgAliasName 机构别名
	 * @param orgDesc 机构说明
	 * @param orgPhone 机构电话
	 * @param orgFax 机构传真
	 * @param orgWebsite 机构网址
	 * @param provinceId 机构省Id
	 * @param cityId 机构市Id
	 * @param districtId 机构区Id
	 * @param orgAddress 机构地址
	 * @param lng 机构经度
	 * @param lat 机构纬度
	 * @param orgManagerId 机构管理人员Id
	 * @param orgManagerName 机构管理人员名称
	 * @param orgManagerPhone 机构管理人员电话
	 * @return DataMessage
	 */
	@ResponseBody
	@PostMapping("/addSub")
	public DataMessage addSub(
			@RequestParam(required = true, value = "orgPid") String orgPid,
			@RequestParam(required = true, value = "orgName") String orgName,
			@RequestParam(required = false, value = "orgAliasName") String orgAliasName,
			@RequestParam(required = false, value = "orgDesc") String orgDesc,
			@RequestParam(required = false, value = "orgPhone") String orgPhone,
			@RequestParam(required = false, value = "orgFax") String orgFax,
			@RequestParam(required = false, value = "orgWebsite") String orgWebsite,
			@RequestParam(required = false, value = "provinceId") String provinceId,
			@RequestParam(required = false, value = "cityId") String cityId,
			@RequestParam(required = false, value = "districtId") String districtId,
			@RequestParam(required = false, value = "orgAddress") String orgAddress,
			@RequestParam(required = false, value = "lng") String lng,
			@RequestParam(required = false, value = "lat") String lat,
			@RequestParam(required = false, value = "orgManagerId") String orgManagerId,
			@RequestParam(required = false, value = "orgManagerName") String orgManagerName,
			@RequestParam(required = false, value = "orgManagerPhone") String orgManagerPhone) {
		// 返回数据消息实体
		DataMessage dm = new DataMessage();
		// 后台数据验证
		if(!this.validateStringProperty(orgPid, 50, true)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("父级机构参数错误");
		} if(!this.validateStringProperty(orgName, 50, true)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构名称参数错误");
		} else if(!this.validateStringProperty(orgAliasName, 50, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构别名别名参数错误");
		} else if(!this.validateStringProperty(orgDesc, 255, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构说明参数错误");
		} else if(!this.validateStringProperty(orgPhone, 50, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构电话参数错误");
		} else if(!this.validateStringProperty(orgFax, 50, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构传真参数错误");
		} else if(!this.validateStringProperty(orgWebsite, 255, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构网址参数错误");
		} else if(!this.validateStringProperty(provinceId, 50, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构省Id参数错误");
		} else if(!this.validateStringProperty(cityId, 50, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构市Id参数错误");
		} else if(!this.validateStringProperty(districtId, 50, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构区Id参数错误");
		} else if(!this.validateStringProperty(orgAddress, 255, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构地址参数错误");
		} else if(!this.validateStringProperty(lng, 15, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构经度参数错误");
		} else if(!this.validateStringProperty(lat, 15, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构纬度参数错误");
		} else if(!this.validateStringProperty(orgManagerId, 50, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构管理人员Id参数错误");
		} else if(!this.validateStringProperty(orgManagerName, 50, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构管理人员名称参数错误");
		} else if(!this.validateStringProperty(orgManagerPhone, 50, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构管理人员电话参数错误");
		} else {
			BigDecimal lngBd = null;
			if(lng != null && !"".equals(lng.trim())) {
				lngBd = new BigDecimal(lng);
				lngBd.setScale(6, BigDecimal.ROUND_HALF_UP);
			}
			BigDecimal latBd = null;
			if(lat != null && !"".equals(lat.trim())) {
				latBd = new BigDecimal(lat);   
				latBd.setScale(6, BigDecimal.ROUND_HALF_UP);
			}
			Long orgManagerIdLong = (orgManagerId == null || "".equals(orgManagerId.trim())) ? null : Long.parseLong(orgManagerId);
			Long orgId = this.organizationService.addSub(Long.parseLong(orgPid), orgName, orgAliasName, orgDesc, orgPhone, orgFax,
					orgWebsite, provinceId, cityId, districtId, orgAddress, lngBd, latBd, orgManagerIdLong, orgManagerName,
					orgManagerPhone);
			dm.setResult(WyConstant.RESULT_CODE_SUCCESS);
			dm.setData(orgId);
			dm.setMessage("子机构添加成功");
		}
		return dm;
	}
	
	/**
	 * 编辑系统机构
	 * @param orgId 机构Id
	 * @param orgName 机构名称
	 * @param orgAliasName 机构别名
	 * @param orgDesc 机构说明
	 * @param orgPhone 机构电话
	 * @param orgFax 机构传真
	 * @param orgWebsite 机构网址
	 * @param provinceId 机构省Id
	 * @param cityId 机构市Id
	 * @param districtId 机构区Id
	 * @param orgAddress 机构地址
	 * @param lng 机构经度
	 * @param lat 机构纬度
	 * @param orgManagerId 机构管理人员Id
	 * @param orgManagerName 机构管理人员名称
	 * @param orgManagerPhone 机构管理人员电话
	 * @return DataMessage
	 */
	@ResponseBody
	@PostMapping("/edit")
	public DataMessage edit(
			@RequestParam(required = true, value = "orgId") String orgId,
			@RequestParam(required = true, value = "orgName") String orgName,
			@RequestParam(required = false, value = "orgAliasName") String orgAliasName,
			@RequestParam(required = false, value = "orgDesc") String orgDesc,
			@RequestParam(required = false, value = "orgPhone") String orgPhone,
			@RequestParam(required = false, value = "orgFax") String orgFax,
			@RequestParam(required = false, value = "orgWebsite") String orgWebsite,
			@RequestParam(required = false, value = "provinceId") String provinceId,
			@RequestParam(required = false, value = "cityId") String cityId,
			@RequestParam(required = false, value = "districtId") String districtId,
			@RequestParam(required = false, value = "orgAddress") String orgAddress,
			@RequestParam(required = false, value = "lng") String lng,
			@RequestParam(required = false, value = "lat") String lat,
			@RequestParam(required = false, value = "orgManagerId") String orgManagerId,
			@RequestParam(required = false, value = "orgManagerName") String orgManagerName,
			@RequestParam(required = false, value = "orgManagerPhone") String orgManagerPhone) {
		// 返回数据消息实体
		DataMessage dm = new DataMessage();
		// 后台数据验证
		if(!this.validateStringProperty(orgId, 50, true)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构Id参数错误");
		} if(!this.validateStringProperty(orgName, 50, true)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构名称参数错误");
		} else if(!this.validateStringProperty(orgAliasName, 50, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构别名别名参数错误");
		} else if(!this.validateStringProperty(orgDesc, 255, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构说明参数错误");
		} else if(!this.validateStringProperty(orgPhone, 50, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构电话参数错误");
		} else if(!this.validateStringProperty(orgFax, 50, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构传真参数错误");
		} else if(!this.validateStringProperty(orgWebsite, 255, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构网址参数错误");
		} else if(!this.validateStringProperty(provinceId, 50, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构省Id参数错误");
		} else if(!this.validateStringProperty(cityId, 50, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构市Id参数错误");
		} else if(!this.validateStringProperty(districtId, 50, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构区Id参数错误");
		} else if(!this.validateStringProperty(orgAddress, 255, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构地址参数错误");
		} else if(!this.validateStringProperty(lng, 15, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构经度参数错误");
		} else if(!this.validateStringProperty(lat, 15, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构纬度参数错误");
		} else if(!this.validateStringProperty(orgManagerId, 50, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构管理人员Id参数错误");
		} else if(!this.validateStringProperty(orgManagerName, 50, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构管理人员名称参数错误");
		} else if(!this.validateStringProperty(orgManagerPhone, 50, false)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构管理人员电话参数错误");
		} else {
			BigDecimal lngBd = null;
			if(lng != null && !"".equals(lng.trim())) {
				lngBd = new BigDecimal(lng);
				lngBd.setScale(6, BigDecimal.ROUND_HALF_UP);
			}
			BigDecimal latBd = null;
			if(lat != null && !"".equals(lat.trim())) {
				latBd = new BigDecimal(lat);   
				latBd.setScale(6, BigDecimal.ROUND_HALF_UP);
			}
			Long orgManagerIdLong = (orgManagerId == null || "".equals(orgManagerId.trim())) ? null : Long.parseLong(orgManagerId);
			this.organizationService.edit(Long.parseLong(orgId), null, null, null, orgName, orgAliasName, orgDesc, orgPhone,
					orgFax, orgWebsite, provinceId, cityId, districtId, orgAddress, lngBd, latBd, orgManagerIdLong,
					orgManagerName, orgManagerPhone);
			dm.setResult(WyConstant.RESULT_CODE_SUCCESS);
			dm.setMessage("机构编辑成功");
		}
		return dm;
	}
	
	/**
	 * 删除系统机构
	 * @param orgId 机构Id
	 * @return DataMessage
	 */
	@ResponseBody
	@PostMapping("/delete")
	public DataMessage delete(@RequestParam(required = true, value = "orgId") String orgId) {
		// 返回数据消息实体
		DataMessage dm = new DataMessage();
		// 后台数据验证
		if(!this.validateStringProperty(orgId, 50, true)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构Id参数错误");
		} else {
			organizationService.editIsDel(Long.parseLong(orgId), 1);
			dm.setResult(WyConstant.RESULT_CODE_SUCCESS);
			dm.setMessage("机构删除成功");
		}
		return dm;
	}
	
	/**
	 * 批量删除系统机构
	 * @param orgIds 机构Ids
	 * @return DataMessage
	 */
	@ResponseBody
	@PostMapping("/batchDelete")
	public DataMessage deleteBatch(@RequestParam(required = true, value = "orgIds") String orgIds) {
		// 返回数据消息实体
		DataMessage dm = new DataMessage();
		// 后台数据验证
		if(!this.validateStringProperty(orgIds, 128, true)) {
			dm.setResult(WyConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("机构Ids参数错误");
		} else {
			String[] orgIdsArr = orgIds.split(",");
			for(String orgId : orgIdsArr) {
				organizationService.editIsDel(Long.parseLong(orgId), 1);
			}
			dm.setResult(WyConstant.RESULT_CODE_SUCCESS);
			dm.setMessage("菜单删除成功");
		}
		return dm;
	}
	
	/**
	 * 导出数据到Excel
	 * @return Excel File
	 * @throws Exception
	 */
	@PostMapping("/export")
    public ModelAndView exportExcel() throws Exception {
        Workbook wb = new HSSFWorkbook();
        Sheet sheet = wb.createSheet("系统机构信息");
        // 手动设置列宽。第一个参数表示要为第几列设；，第二个参数表示列的宽度，n为列高的像素数。
        sheet.setColumnWidth((short) 0, (short) (15.7 * 150));
        sheet.setColumnWidth((short) 1, (short) (25.7 * 150));
        sheet.setColumnWidth((short) 2, (short) (15.7 * 150));
        sheet.setColumnWidth((short) 3, (short) (35.7 * 150));
        sheet.setColumnWidth((short) 4, (short) (35.7 * 150));
        sheet.setColumnWidth((short) 5, (short) (35.7 * 150));
        sheet.setColumnWidth((short) 6, (short) (35.7 * 150));
        sheet.setColumnWidth((short) 7, (short) (35.7 * 150));
        sheet.setColumnWidth((short) 8, (short) (35.7 * 150));
        // 创建第一行
        Row row = sheet.createRow((short) 0);
        // 创建两种单元格格式
        CellStyle cs = wb.createCellStyle();
        CellStyle cs2 = wb.createCellStyle();
        
        // 创建两种字体
        Font f = wb.createFont();
        Font f2 = wb.createFont();
        
        // 创建第一种字体样式
        f.setFontHeightInPoints((short) 12);
        f.setColor(IndexedColors.BLACK.getIndex());
        f.setBoldweight(Font.BOLDWEIGHT_BOLD);
        // 创建第二种字体样式
        f2.setFontHeightInPoints((short) 11);
        f2.setColor(IndexedColors.BLACK.getIndex());
        f2.setBoldweight(Font.BOLDWEIGHT_NORMAL);
        
        // 设置第一种单元格的样式
        cs.setFont(f);
        cs.setBorderLeft(CellStyle.BORDER_THIN);
        cs.setBorderRight(CellStyle.BORDER_THIN);
        cs.setBorderTop(CellStyle.BORDER_THIN);
        cs.setBorderBottom(CellStyle.BORDER_THIN);
        cs.setAlignment(HSSFCellStyle.ALIGN_LEFT);
        
        // 设置第二种单元格的样式
        cs2.setFont(f2);
        cs2.setBorderLeft(CellStyle.BORDER_THIN);
        cs2.setBorderRight(CellStyle.BORDER_THIN);
        cs2.setBorderTop(CellStyle.BORDER_THIN);
        cs2.setBorderBottom(CellStyle.BORDER_THIN);
        cs2.setAlignment(HSSFCellStyle.ALIGN_LEFT);
        
        // 创建列（每行里的单元格）
        Cell cell = row.createCell(0);
        cell.setCellValue("序号");
        cell.setCellStyle(cs);
        
        cell = row.createCell(1);
        cell.setCellValue("机构编码");
        cell.setCellStyle(cs);
        
        cell = row.createCell(2);
        cell.setCellValue("机构级别");
        cell.setCellStyle(cs);
        
        cell = row.createCell(3);
        cell.setCellValue("机构名称");
        cell.setCellStyle(cs);
        
        cell = row.createCell(4);
        cell.setCellValue("机构别名");
        cell.setCellStyle(cs);
        
        cell = row.createCell(5);
        cell.setCellValue("机构说明");
        cell.setCellStyle(cs);
        
        cell = row.createCell(6);
        cell.setCellValue("机构电话");
        cell.setCellStyle(cs);
        
        cell = row.createCell(7);
        cell.setCellValue("机构网址");
        cell.setCellStyle(cs);
        
        cell = row.createCell(8);
        cell.setCellValue("机构地址");
        cell.setCellStyle(cs);
        
        PageHelper.orderBy("org_code");
        List<Organization> orgList = this.organizationService.queryList(null, null, getOrgType(), null, 0,
				null, null, null, null, null, null, null, null, null, null,
				null, null, null, null, null, null, null, null, null,
				null, null);
    	for(int i = 0; i < orgList.size(); i++) {
    		Organization org = orgList.get(i);
    		row = sheet.createRow(i + 1);
    		cell = row.createCell(0);
    		cell.setCellValue(i + 1);
    		cell.setCellStyle(cs2);
    		  
    		cell = row.createCell(1);
    		cell.setCellValue(org.getOrgCode());
    		cell.setCellStyle(cs2);
    		  
    		cell = row.createCell(2);
    		cell.setCellValue(org.getOrgLevel());
    		cell.setCellStyle(cs2);
    		  
    		cell = row.createCell(3);
    		cell.setCellValue(org.getOrgName());
    		cell.setCellStyle(cs2);
    		  
    		cell = row.createCell(4);
    		cell.setCellValue((org.getOrgAliasName() == null || "".equals(org.getOrgAliasName().trim())) ? "—" : org.getOrgAliasName());
    	    cell.setCellStyle(cs2);
    		  
    		cell = row.createCell(5);
    		cell.setCellValue((org.getOrgDesc() == null || "".equals(org.getOrgDesc().trim())) ? "—" : org.getOrgDesc());
    		cell.setCellStyle(cs2);
    		  
    		cell = row.createCell(6);
    		cell.setCellValue((org.getOrgPhone() == null || "".equals(org.getOrgPhone().trim())) ? "—" : org.getOrgPhone());
    		cell.setCellStyle(cs2);
    		  
    		cell = row.createCell(7);
    		cell.setCellValue((org.getOrgWebsite() == null || "".equals(org.getOrgWebsite().trim())) ? "—" : org.getOrgWebsite());
    		cell.setCellStyle(cs2);
    		  
    		cell = row.createCell(8);
    		cell.setCellValue((org.getOrgAddress() == null || "".equals(org.getOrgAddress().trim())) ? "—" : org.getOrgAddress());
    		cell.setCellStyle(cs2);
    	}
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            wb.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] content = os.toByteArray();
        InputStream is = new ByteArrayInputStream(content);
        response.reset();
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + new String(("系统机构信息" + ".xls").getBytes(), "ISO-8859-1"));
        ServletOutputStream out = response.getOutputStream();
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(is);
            bos = new BufferedOutputStream(out);
            byte[] buff = new byte[2048];
            int bytesRead;
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);
            }
        } catch (final IOException e) {
            throw e;
        } finally {
            if (bis != null)
                bis.close();
            if (bos != null)
                bos.close();
        }
        return null;
	}
	
	/**
	 * 递归添加机构子集
	 * @param resultList 结果List
	 * @param parentOrg 父级机构
	 */
	private void recursionSubOrg(List<Organization> resultList, Organization parentOrg) {
		if(parentOrg.getIsParent().intValue() == 1) {
			List<Organization> subList = this.organizationService.querySubList(parentOrg.getOrgId());
			parentOrg.setSubList(subList);
			resultList.add(parentOrg);
			for(Organization sub : subList) {
				if(sub.getIsParent().intValue() == 1) {
					recursionSubOrg(resultList, sub);
				}
			}
		} else {
			resultList.add(parentOrg);
		}
	}
	
}